﻿Imports System.ComponentModel
Imports DevExpress.LookAndFeel
Imports System.Linq
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Columns
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Data
Imports System.Reflection

Public Class fOrcamentoManutVendrdor
    Public db As New LinqGeralDataContext
    Public ANOORC As Integer
    Sub New()
        InitSkins()
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub
    Sub InitSkins()
        DevExpress.Skins.SkinManager.EnableFormSkins()
        DevExpress.UserSkins.BonusSkins.Register()
        UserLookAndFeel.Default.SetSkinStyle("Metropolis")
    End Sub

    Private Sub fOrcamentoManutVendrdor_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim ListaAno = (From c In db.tblOrcamentos Group By c.ANO Into Group _
                      Select ANO)

        For Each anoins In ListaAno
            ComboBoxEdit1.Properties.Items.Add(anoins.ToString)
        Next
        ComboBoxEdit1.SelectedIndex = ListaAno.Count

        ANOORC = ComboBoxEdit1.SelectedItem.ToString


        Me.OrcamentoVendedorTableAdapter.Fill(DsManutOrcamento.OrcamentoVendedor, GlobalVariables.Empresa, GlobalVariables.Vendedor, ANOORC)


        Me.TblCustomerTableAdapter.Fill(DsManutOrcamento.tblCustomer, GlobalVariables.Vendedor, GlobalVariables.Empresa)

        Me.TblItemTableAdapter.Fill(Me.DsManutOrcamento.tblItem, GlobalVariables.Empresa)

        GridView1.BestFitColumns()
    End Sub

    Private Sub GridView1_CellValueChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles GridView1.CellValueChanged
        Dim view As GridView = TryCast(sender, GridView)
        If view.IsNewItemRow(e.RowHandle) Then
            If e.Column.Name = colCODCLI.Name Then
                Dim NomeCliente = (From c In db.tblCustomer1s _
                                 Where c.No_ = e.Value.ToString AndAlso c.Company = GlobalVariables.Empresa _
                                 Select c.Name).SingleOrDefault

                GridView1.SetFocusedRowCellValue(colDESCCLI, NomeCliente.ToString)

            End If

            If e.Column.Name = colCODITEM.Name Then
                Dim NomeItem = (From c In db.tblItems _
                                 Where c.No_ = e.Value.ToString AndAlso c.Company = GlobalVariables.Empresa _
                                 Select c.Extended_Description).SingleOrDefault

                GridView1.SetFocusedRowCellValue(colDESCITEM, NomeItem.ToString)

            End If
        Else
            If e.Column.Name = colPOTENCIAL1.Name Then


                Dim potencial = (From c In db.tblOrcamentos _
                Where c.CODPROD = GridView1.GetFocusedRowCellValue(colCODITEM).ToString AndAlso c.CODCLI = GridView1.GetFocusedRowCellValue(colCODCLI).ToString AndAlso c.ANO = ANOORC AndAlso c.Armazem = GridView1.GetFocusedRowCellValue(colARMAZEM).ToString _
                Select c).SingleOrDefault

                With potencial
                    .POTENCIAL = GridView1.GetFocusedRowCellValue(colPOTENCIAL1)
                    .Utilizador = GlobalVariables.UserName
                End With

                db.SubmitChanges()

            End If

            If e.Column.Name = PVDORC.Name Then

                Dim potencial = (From c In db.tblOrcamentos _
                Where c.CODPROD = GridView1.GetFocusedRowCellValue(colCODITEM).ToString AndAlso c.CODCLI = GridView1.GetFocusedRowCellValue(colCODCLI).ToString AndAlso c.ANO = ANOORC AndAlso c.Armazem = GridView1.GetFocusedRowCellValue(colARMAZEM).ToString _
                Select c).SingleOrDefault

                With potencial
                    .PVD_ORC = GridView1.GetFocusedRowCellValue(PVDORC).ToString
                    .Utilizador = GlobalVariables.UserName
                End With

                db.SubmitChanges()

            End If

            If e.Column.Name = QDORC.Name Then

                Dim potencial = (From c In db.tblOrcamentos _
                Where c.CODPROD = GridView1.GetFocusedRowCellValue(colCODITEM).ToString AndAlso c.CODCLI = GridView1.GetFocusedRowCellValue(colCODCLI).ToString AndAlso c.ANO = ANOORC AndAlso c.Armazem = GridView1.GetFocusedRowCellValue(colARMAZEM).ToString _
                Select c).SingleOrDefault

                With potencial
                    .QD_ORC = GridView1.GetFocusedRowCellValue(QDORC).ToString
                    .Utilizador = GlobalVariables.UserName
                End With

                db.SubmitChanges()

            End If


        End If
    End Sub

    Private Sub ComboBoxEdit1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxEdit1.SelectedIndexChanged
        If ComboBoxEdit1.SelectedItem <> "" Then
            ANOORC = ComboBoxEdit1.SelectedItem.ToString

            Me.OrcamentoVendedorTableAdapter.Fill(DsManutOrcamento.OrcamentoVendedor, GlobalVariables.Empresa, GlobalVariables.Vendedor, ANOORC)
        End If
        
    End Sub


    Private Sub GridView1_FocusedRowChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles GridView1.FocusedRowChanged
        If e.FocusedRowHandle.ToString < 0 Then
            colCODCLI.OptionsColumn.ReadOnly = False
            colCODITEM.OptionsColumn.ReadOnly = False
            colARMAZEM.OptionsColumn.ReadOnly = False
            colCODCLI.OptionsColumn.AllowFocus = True
            colCODITEM.OptionsColumn.AllowFocus = True
            colARMAZEM.OptionsColumn.AllowFocus = True
            colCODCLI.OptionsColumn.AllowEdit = True
            colCODITEM.OptionsColumn.AllowEdit = True
            colARMAZEM.OptionsColumn.AllowEdit = True

        Else
            colCODCLI.OptionsColumn.ReadOnly = True
            colCODITEM.OptionsColumn.ReadOnly = True
            colARMAZEM.OptionsColumn.ReadOnly = True
            colCODCLI.OptionsColumn.AllowFocus = False
            colCODITEM.OptionsColumn.AllowFocus = False
            colARMAZEM.OptionsColumn.AllowFocus = False
            colCODCLI.OptionsColumn.AllowEdit = False
            colCODITEM.OptionsColumn.AllowEdit = False
            colARMAZEM.OptionsColumn.AllowEdit = False
        End If

      
    End Sub

    

    Private Sub GridView1_ValidateRow1(sender As Object, e As DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs) Handles GridView1.ValidateRow
        Dim view As GridView = TryCast(sender, GridView)
        If view.IsNewItemRow(e.RowHandle) Then
            Dim columnPVD As GridColumn = view.Columns("PVD-ORC")
            Dim valPVD As Object = view.GetRowCellValue(e.RowHandle, columnPVD)
            If valPVD Is Nothing OrElse valPVD.ToString() = String.Empty Then
                e.Valid = False
                e.ErrorText = "Preço de Venda é obrigatório."
                view.SetColumnError(columnPVD, "Campo não pode estar Vazio")
            End If
            Dim columnQD As GridColumn = view.Columns("QD-ORC")
            Dim valQD As Object = view.GetRowCellValue(e.RowHandle, columnQD)
            If valQD Is Nothing OrElse valQD.ToString() = String.Empty Then
                e.Valid = False
                e.ErrorText = "Quantidade é obrigatório."
                view.SetColumnError(columnQD, "Campo não pode estar Vazio")
            End If
            Dim columnARMAZEM As GridColumn = view.Columns("Armazem")
            Dim valARMAZEM As Object = view.GetRowCellValue(e.RowHandle, columnARMAZEM)
            If valARMAZEM Is Nothing OrElse valARMAZEM.ToString() = String.Empty Then
                e.Valid = False
                e.ErrorText = "Armazem é obrigatório."
                view.SetColumnError(columnARMAZEM, "Campo não pode estar Vazio")
            End If
            Dim columnCODITEM As GridColumn = view.Columns("CODPROD")
            Dim valCODITEM As Object = view.GetRowCellValue(e.RowHandle, columnCODITEM)
            If valCODITEM Is Nothing OrElse valCODITEM.ToString() = String.Empty Then
                e.Valid = False
                e.ErrorText = "Código de Artigo é obrigatório."
                view.SetColumnError(columnCODITEM, "Campo não pode estar Vazio")
            End If
            Dim columnCODCLI As GridColumn = view.Columns("CODCLI")
            Dim valCODCLI As Object = view.GetRowCellValue(e.RowHandle, columnCODCLI)
            If valCODCLI Is Nothing OrElse valCODCLI.ToString() = String.Empty Then
                e.Valid = False
                e.ErrorText = "Código de Cliente é obrigatório."
                view.SetColumnError(columnCODCLI, "Campo não pode estar Vazio")
            End If
            
            Dim ValorPotencial As Integer
            Dim columnPOTENCIAL As GridColumn = view.Columns("POTENCIAL")
            Dim valPOTENCIAL As Object = view.GetRowCellValue(e.RowHandle, columnPOTENCIAL)
            If valPOTENCIAL Is Nothing OrElse valPOTENCIAL.ToString() = String.Empty Then
                'e.Valid = False
                'e.ErrorText = "Preço de Venda é obrigatório."
                'view.SetColumnError(columnPVD, "Campo não pode estar Vazio")
                ValorPotencial = 0
            Else
                ValorPotencial = GridView1.GetFocusedRowCellValue(colPOTENCIAL1)
            End If
            Dim verifica As Integer = -1
            If e.Valid.ToString = True Then
                Dim verificar = (From c In db.tblOrcamentos _
                              Where c.ANO = ANOORC AndAlso c.Armazem = GridView1.GetFocusedRowCellValue(colARMAZEM).ToString AndAlso c.CODCLI = GridView1.GetFocusedRowCellValue(colCODCLI).ToString AndAlso c.CODPROD = GridView1.GetFocusedRowCellValue(colCODITEM).ToString _
                              Select c.CODCLI).Count
                verifica = verificar
            End If
            If e.Valid.ToString = True And verifica = 0 Then
                Dim linha As New tblOrcamento With _
    {.CODCLI = GridView1.GetFocusedRowCellValue(colCODCLI), .CODPROD = GridView1.GetFocusedRowCellValue(colCODITEM), .QD_ORC = GridView1.GetFocusedRowCellValue(QDORC), .PVD_ORC = GridView1.GetFocusedRowCellValue(PVDORC), .ANO = ANOORC, .POTENCIAL = ValorPotencial, .Utilizador = GlobalVariables.UserName, .Armazem = GridView1.GetFocusedRowCellValue(colARMAZEM)}
                db.tblOrcamentos.InsertOnSubmit(linha)
                db.SubmitChanges()
            ElseIf e.Valid.ToString = True And verifica > 0 Then
                e.Valid = False
                e.ErrorText = "Linha já existe"
                view.SetColumnError(columnCODCLI, "Linha já existe.")

            End If

        End If

    End Sub
End Class